package com.yb0os1.serve.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yb0os1.constant.MessageConstant;
import com.yb0os1.constant.PasswordConstant;
import com.yb0os1.constant.StatusConstant;
import com.yb0os1.context.BaseContext;
import com.yb0os1.dto.EmployeeDTO;
import com.yb0os1.dto.EmployeeEditPasswordDTO;
import com.yb0os1.dto.EmployeeLoginDTO;
import com.yb0os1.entity.Employee;
import com.yb0os1.exception.AccountLockedException;
import com.yb0os1.exception.AccountNotFoundException;
import com.yb0os1.exception.PasswordEditFailedException;
import com.yb0os1.exception.PasswordErrorException;
import com.yb0os1.mapper.EmployeeMapper;
import com.yb0os1.result.PageResult;
import com.yb0os1.result.Result;
import com.yb0os1.serve.EmployeeServe;
import com.yb0os1.utils.AliOssUtil;
import com.yb0os1.utils.SHA256Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class EmployeeServerImpl implements EmployeeServe {
    @Autowired
    private EmployeeMapper employeeMapper;

    /***
     * 修改员工密码
     * @param employeeEditPasswordDTO
     */
    @Override
    public void editPassword(EmployeeEditPasswordDTO employeeEditPasswordDTO) {
        String oldPassword=employeeEditPasswordDTO.getOldPassword();
        String newPassword = employeeEditPasswordDTO.getNewPassword();
        Long empId = employeeEditPasswordDTO.getEmpId();
        if (!employeeMapper.getById(empId).getPassword().equals(SHA256Utils.sha256(oldPassword))){
            //输入的旧密码和数据库密码不一致
            throw new PasswordEditFailedException("输入原密码错误");
        }
        Employee employee = new Employee();
        employee.setId(empId);
        employee.setPassword(SHA256Utils.sha256(newPassword));
//        employee.setUpdateUser(BaseContext.getCurrentId());
//        employee.setUpdateTime(LocalDateTime.now());
        employeeMapper.update(employee);
    }

    /***
     * 根据id查找员工
     * @param id
     * @return
     */
    @Override
    public Result<Employee> getById(Long id) {
        Employee employee =  employeeMapper.getById(id);
        return Result.success(employee);
    }

    /***
     * 编辑员工信息
     * @param employeeDTO
     */
    @Override
    public void updateUser(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.update(employee);
    }

    /***
     * 启动或者禁用员工账号
     * @param status
     * @param id
     */
    @Override
    public void isOpenCloseAccount(Integer status, Long id) {
        //使用实体类  写一个通用的update方法
        Employee employee = new Employee();
        employee.setId(id);
        employee.setStatus(status);
//        employee.setUpdateUser(BaseContext.getCurrentId());
//        employee.setUpdateTime(LocalDateTime.now());
        log.info("员工{}",employee);
        employeeMapper.update(employee);
    }

    @Override
    public PageResult pageSelect(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Employee> employeeList =  employeeMapper.selectAll(name);
        Page<Employee> p = (Page<Employee>) employeeList;
        return new PageResult(p.getTotal(),p.getResult());
    }

    /***
     * 新增员工
     * @param employeeDTO
     */
    @Override
    public void addEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //属性拷贝
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setPassword(SHA256Utils.sha256(PasswordConstant.DEFAULT_PASSWORD));

//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateUser(BaseContext.getCurrentId());
//        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setStatus(StatusConstant.ENABLE);

        employeeMapper.insertEmployee(employee);
    }


    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */

    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // sha256加密对比
        password = SHA256Utils.sha256(password);
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (Objects.equals(employee.getStatus(), StatusConstant.DISABLE)) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }
}
